package cl.movix.usuarios.benchmarks.migration;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

import src.cl.movix.repositories.Repository;
import src.cl.movix.repositories.RepositoryAccess;
import src.cl.movix.repositories.RepositoryException;
import src.cl.movix.repositories.RepositoryType;
import src.cl.movix.repositories.SQLRepository;
import cl.movix.model.customer.ICustomer;
import cl.movix.usuarios.benchmarks.providers.DataBaseCustomerProvider;
import cl.movix.usuarios.benchmarks.providers.NoNextCustomerException;
import cl.movix.usuarios.benchmarks.rates.Rateable;
import cl.movix.usuarios.benchmarks.rates.Rater;

public class SuscriptionMigration implements Rateable {

	private static Logger logger = Logger.getLogger(SuscriptionMigration.class);

	/* local en Movix: 192.168.1.188; local en casa: 192.168.0.16 */
	private static final String POSTGRESQL_HOST = "10.5.5.133";

	private long migratedCustomers;

	private long targetMigration;

	private Repository postgreSQLRepository;

	private RepositoryAccess access;

	private Rater rater;

	public SuscriptionMigration(RepositoryAccess access)
			throws RepositoryException {

		this.access = access;
		this.postgreSQLRepository = new SQLRepository(access);

		this.migratedCustomers = 0;
		this.targetMigration = this.postgreSQLRepository
				.countTable("suscripciones");

		this.rater = new Rater(this, 5000);
		rater.setTargetTransaccionts(this.targetMigration);
	}

	/**
	 * @param args
	 * @throws RepositoryException
	 */
	public static void main(String[] args) throws RepositoryException {
		/* Basic configuration for Log4J is performed */
		BasicConfigurator.configure();

		/* A connection profile is created for postgreSQL */
		RepositoryAccess postSQLAccess = new RepositoryAccess(
				RepositoryType.PostgreSQL, org.postgresql.Driver.class,
				"postgresql", POSTGRESQL_HOST, 5432, "users", "moduser",
				"proxy2012");

		SuscriptionMigration migrator = new SuscriptionMigration(postSQLAccess);

		migrator.migrate();
	}

	public long getTargetMigration() {
		return targetMigration;
	}

	public long getMigratedCustomers() {
		return migratedCustomers;
	}

	private void migrate() throws RepositoryException {

		/* The users are deleted */
		//this.postgreSQLRepository.deleteUsuarios();

		DataBaseCustomerProvider customerProvider = new DataBaseCustomerProvider(
				this.access, "suscripciones", createColumnMapping());

		logger.info("Starting to migrate the customers and its properties");
		rater.startRater();

		/* Set the use of cursor */
		while (customerProvider.hasNext()) {
			/* A user is obtained from the result set */
			ICustomer customer;
			try {
				customer = customerProvider.nextCustomer();
			} catch (NoNextCustomerException e) {
				logger.error(
						"Weird behaviour, the customers where unexpectedly end.",
						e);
				break;
			}

			this.postgreSQLRepository.insert(customer);
			this.migratedCustomers++;
		}

		rater.stopRater();

	}

	/**
	 * TODO
	 * 
	 * @return
	 */
	private static Map<String, String> createColumnMapping() {

		/* A new hashmap is created */
		Map<String, String> columnMapping = new HashMap<String, String>();

		/* The mapping is set */
		columnMapping.put("id", "SUSCRIPCION_ID");
		columnMapping.put("movil", "MSISDN");
		columnMapping.put("comentario", "SUSCRIPCION_COMMENT");
		columnMapping.put("activa", "SUS_ACTIVE");
		columnMapping.put("dias_para_expirar", "SUS_DIAS_PARA_EXPIRAR");

		/* The map is returned */
		return columnMapping;
	}

	@Override
	public long getValue() {
		return this.migratedCustomers;
	}
}
